package org.itark.bbs.common.api;

import lombok.Getter;
import lombok.Setter;
import org.itark.bbs.common.constants.enums.IErrors;
import org.itark.bbs.common.constants.enums.ResponseCode;

import java.io.Serializable;

/**
 * @author ：lishenglei
 * @date ：Created in 2021/6/30 17:58
 * @description：
 */
@Setter
@Getter
public class RestResult<T> implements Serializable {
    private static final long serialVersionUID = 6095433538316185018L;

    private int code;
    private String message;
    private boolean success;
    private T data;
    private long timestamp = System.currentTimeMillis();

    private RestResult() {
    }

    private RestResult(int code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public static RestResult<String> success(){
        return new RestResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), true, "");
    }

    public static <T> RestResult<T> success(T data){
        return new RestResult<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), true, data);
    }

    public static RestResult<Object> failed(String msg, Object data) {
        return new RestResult<>(ResponseCode.FAILED.getCode(), msg, false, data);
    }

    public static RestResult<Object> failed(IErrors errors) {
        return new RestResult<>(errors.getCode(), errors.getMessage(), false, null);
    }

}
